<?php
App::uses('AppHelper', 'View/Helper');
/**
 * LinkHelper class
 * Help building left menu by context
 * 
 * Ex: In Controller you can set each method to automatic build left menu by context
 * public function add() {
 * 	//Begin Enable menu for this function
 * 	$menu = array('Home'=>'http://advertising.ai-t.mobi', 'Test'=>'http://advertising.ai-t.mobi');
 * 	$this->set("menu", $menu);
 * 	//End
 * 	...
 * }
 */
class LinkHelper extends AppHelper {
	var $helpers = array('Html');
	var $enableMenu = true;
	var $_menus = array();

	/**
	 * Generate html menu
	 */
	public function generate(){
		$outputHtml = "";
		$menus = $this->_View->get("menu");
		if($menus){
			$count = 0;
			foreach ($menus as $title=> $link) {
				if($count == 0){
					$outputHtml = $outputHtml ."<h3>$title</h3>";
					$outputHtml = $outputHtml ."<div id=\"left-menu\">";
				}
				else
				{
					$outputHtml = $outputHtml . '<div class="left-menu-item"><li><a href="'.$link.'">' . $title . '</a></li></div>';
				}
				$count++;
			}
			$outputHtml = $outputHtml ."</div>";
		}
		return $outputHtml;
	}

	/**
	 * Get menu items
	 * @return Return array include all menu item
	 */
	public function getMenus(){
		return $this->_View->get("menu");
	}
	
	public function selectedMenu($currentUrl, $rootMenus = array()){
		foreach ($rootMenus as $menu){
			$pos = strpos($currentUrl, $menu);
			if($pos !== false){
				return "active";
			}
		}
		return "empty";
	}
	
	public function frontendSelectedMenuFirst($currentUrl, $rootMenus = array()){
		foreach ($rootMenus as $menu){
			$pos = strpos($currentUrl, $menu);
			if($pos !== false){
				return "on";
			}
		}
		return "";
	}
	
	public function frontendSelectedMenuSecond($currentUrl, $rootMenus = array()){
		foreach ($rootMenus as $menu){
			$pos = strpos($currentUrl, $menu);
			if($pos !== false){
				return "on2";
			}
		}
		return "";
	}
    public function unLoginSelectedMenu($currentUrl, $rootMenus = array()){
        if(!empty($rootMenus)){
    		foreach ($rootMenus as $menu){
    			$pos = strpos($currentUrl, $menu);
    			if($pos !== false){
    				return "active";
    			}
    		}
        }
		return "";
	}
	
}